package com.powerunion.datacollection.web.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.powerunion.datacollection.service.FormFieldManager;
import com.powerunion.datacollection.transaction.Transaction;
import com.powerunion.datacollection.transaction.TransactionHelper;
import com.powerunion.util.log.LogHelper;

public class FormFieldAction extends DispatchAction {

	private static final Logger logger = LogHelper.getInstance().getLogger(FormFieldAction.class);
	private  FormFieldManager formFieldManager = null;
	
	public FormFieldAction() {
		super();
		this.formFieldManager = new FormFieldManager();
	}
	
	public void finalize() throws Throwable {
		this.formFieldManager = null;
		super.finalize();
	}
	
	public ActionForward save (ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response)
	        throws Exception {
		
		String saveStr = request.getParameter("formData");
		String formId = request.getParameter("formId");

		Transaction transaction = null;
		try {
			transaction = TransactionHelper.getTransaction();
			transaction.begin();
			logger.debug("主表报送区表单域定义信息:"+saveStr);
			
			boolean flag = this.formFieldManager.save(saveStr,formId);
			
	        if(flag) {
	        	response.getWriter().write("{success:true}");
	        }else{
	        	response.getWriter().write("{failure:true}");
	        }
			
			transaction.commit();
		} catch (Exception e) {
			logger.error("", e);
			try {
				transaction.rollback();
			} catch (Exception e1) {
				logger.error("", e1);
				throw e;
			} finally {
				transaction = null;
				response.getWriter().write("{failure:true}");
			}
		}

		return null;
	}
	
	public ActionForward getFormFields(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String formId = request.getParameter("formId");
		String result = this.formFieldManager.getFormFields(formId);
		logger.debug("FormFields:"+result);
		response.setContentType("text/json;charset=utf-8");
		try{
			response.getWriter().print(result);
		}catch(Exception e) {
			logger.error("输出json失败",e);
			return null;
		}
		return null;
	}
	
	public ActionForward getServers(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String result = "";
		result = this.formFieldManager.getServers();
		logger.debug("getServers:"+result);
		response.setContentType("text/json;charset=utf-8");
		try {
			response.getWriter().print(result);
		} catch (Exception e) {
			logger.error("输出json失败", e);
			return null;
		}
		return null;
	}

	
	
}
